package cn.tedu.carhome.service.impl;

import cn.tedu.carhome.ex.ServiceException;
import cn.tedu.carhome.mapper.SupplierMapper;
import cn.tedu.carhome.pojo.VO.SupplierVO;
import cn.tedu.carhome.pojo.dto.SupplierInsertDTO;
import cn.tedu.carhome.service.ISupplierService;
import cn.tedu.carhome.web.JsonResult;
import cn.tedu.carhome.web.State;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SupplierService implements ISupplierService {
    @Autowired
    SupplierMapper mapper;

    @Override
    public JsonResult<Void> SupplierInsert(SupplierInsertDTO supplierInsertDTO) {
           SupplierVO sVO =  mapper.supplierSelectByName(supplierInsertDTO.getName());
           if(sVO==null){
               int word = mapper.supplierInsert(supplierInsertDTO);
               if (word!=1){
                   throw new ServiceException(State.ERR_INSERT,"添加供应商失败");
               }
               return JsonResult.ok();
           }else {
               throw new ServiceException(State.ERR_INSERT,"供应商已存在");
           }
        }

    @Override
    public JsonResult<SupplierVO> supplierByName(String name) {
        SupplierVO svo = mapper.supplierSelectByName(name);
        if (svo==null){
            throw new ServiceException(State.ERR_BAD_REQUEST,"没有该供应商");
        }
        return JsonResult.ok(svo);
    }

    @Override
    public JsonResult<List<SupplierVO>> supplierSelect() {
        List<SupplierVO> svoList = mapper.supplierSelect();
        return JsonResult.ok(svoList);
    }
}

